//
//  WmcFeedbackInputCellImageCell.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/1.
//

import UIKit

class WmcFeedbackInputCellImageCell: WmcBaseCollectionCell {
    // Internal

    var addClosure: ((WmcFeedbackImageModel) -> Void)?
    var delClosure: ((WmcFeedbackImageModel, IndexPath) -> Void)?

    override func makeUI() {
        imageView.layer.cornerRadius = 4
        imageView.layer.masksToBounds = true
        imageView.contentMode = .scaleToFill
        addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.left.bottom.equalTo(0)
            make.right.equalTo(-10)
            make.top.equalTo(10)
        }

        contentView.addSubview(delBtn)
        delBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.right.equalTo(0)
            make.width.height.equalTo(16)
        }

        contentView.addSubview(addBtn)
        addBtn.snp.makeConstraints { make in
            make.left.bottom.equalTo(0)
            make.right.equalTo(-10)
            make.top.equalTo(10)
        }

        imageView.isHidden = true

        bindEvent()
    }

    func configModel(_ model: WmcFeedbackImageModel, indexPath: IndexPath) {
        self.model = model
        self.indexPath = indexPath
        
        if model.style == .none {
            imageView.isHidden = true
            delBtn.isHidden = true
            addBtn.isHidden = true
        } else if model.style == .add {
            addBtn.isHidden = false
            imageView.isHidden = true
            delBtn.isHidden = true
        } else if model.style == .image {
            addBtn.isHidden = true
            imageView.isHidden = false
            delBtn.isHidden = false
            imageView.image = model.image
        }
    }

    // Private

    private lazy var delBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setBackgroundImage("feedback_del_image".wmc_image, for: .normal)
        return btn
    }()

    private lazy var addBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setImage("feedback_add_image".wmc_image, for: .normal)
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.wmc_grayH15_E6E6E6.cgColor
        return btn
    }()

    private var model: WmcFeedbackImageModel?
    
    private var indexPath: IndexPath?

    private func bindEvent() {
        delBtn.rx.controlEvent(.touchUpInside).subscribe { [weak self] _ in
            guard let weakSelf = self, let model = weakSelf.model, let indexPath = weakSelf.indexPath else { return }
            weakSelf.delClosure?(model, indexPath)
        }.disposed(by: rx.disposeBag)
        
        addBtn.rx.controlEvent(.touchUpInside).subscribe { [weak self] _ in
            guard let weakSelf = self, let model = weakSelf.model else { return }
            weakSelf.addClosure?(model)
        }.disposed(by: rx.disposeBag)
    }
}
